package com.imooc.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.dao.CategoryMapper;
import com.imooc.mall.model.pojo.Category;
import com.imooc.mall.model.request.AddCategoryReq;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.vo.CategoryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;


    @Override
    public void add(AddCategoryReq categoryReq){
        Category category = new Category();
        BeanUtils.copyProperties(categoryReq,category);
        //判断是否已经被注册
        Category categoryOld = categoryMapper.selectByName(categoryReq.getName());
        if (categoryOld!=null){
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTEDS);
        }
        //添加注册
        int i = categoryMapper.insertSelective(category);
        if (i==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.CREATE_FAILED);
        }
    }

    /**
     * 更改商品信息
     */
    @Override
    public void update(Category updateCategoryReq){
        //先判断当前需要更改的在数据库不存在
        Category categoryOld = categoryMapper.selectByName(updateCategoryReq.getName());
        if (categoryOld!=null &&  !categoryOld.getId().equals(updateCategoryReq.getId())){
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTEDS);
        }
        //在进行更改
        int i = categoryMapper.updateByPrimaryKeySelective(updateCategoryReq);
        if (i==0){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }

    /**
     * 删除商品功能
     */
    @Override
    public void delete(Integer id){
        //先查询是否有对应的id
        Category categoryOld = categoryMapper.selectByPrimaryKey(id);
        if (categoryOld==null){
            throw new ImoocMallException(ImoocMallExceptionEnum. DELETE_FAILED);
        }
        int i = categoryMapper.deleteByPrimaryKey(id);
        if (i==0){
            throw new ImoocMallException(ImoocMallExceptionEnum. DELETE_FAILED);
        }
    }

    /**
     * 分页查询功能
     */
    @Override
    public PageInfo listForAdmin(Integer pageNum, Integer pageSize){
        //调用分页查询功能
        PageHelper.startPage(pageNum,pageSize,"type,order_num");
        List<Category> categoryList = categoryMapper.selectList();
        PageInfo pageInfo = new PageInfo(categoryList);
        return pageInfo;

    }


    /**
     * 前台分页查询展示页面
     */

    @Override
    @Cacheable(value = "listCategoryForCustomer")
    public List<CategoryVo> listCategoryForCustomer(Integer parentId){
        ArrayList<CategoryVo> categoryVoList = new ArrayList<>();
        recursivelyFindCategories(categoryVoList,parentId);
        return categoryVoList;
    }

    private void recursivelyFindCategories(List<CategoryVo> categoryVoList,
                                           Integer parentId){
        //递归获取所有子类别，并组合成一个目录树
        List<Category> categoryList = categoryMapper.selectCategoriesByParentId(parentId);
        if (!CollectionUtils.isEmpty(categoryList)){
            for (int i = 0; i < categoryList.size(); i++) {
                Category category =  categoryList.get(i);
                CategoryVo categoryVo = new CategoryVo();
                BeanUtils.copyProperties(category,categoryVo);
                categoryVoList.add(categoryVo);
                recursivelyFindCategories(categoryVo.getChildrenCategory(),categoryVo.getId());
            }
        }
    }
}
